package com.tangcco.lockwell.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tangcco.lockwell.domain.Question;
import com.tangcco.lockwell.domain.VocabularyList;
import com.tangcco.lockwell.domain.dto.TextContentDto;
import com.tangcco.lockwell.repository.VocabularyListMapper;
import com.tangcco.lockwell.service.QuestionService;
import com.tangcco.lockwell.service.VocabularyListService;
import com.tangcco.lockwell.utility.JsonUtil;
import com.tangcco.lockwell.utility.ModelMapperSingle;
import com.tangcco.lockwell.viewmodel.admin.question.QuestionEditRequestVM;
import com.tangcco.lockwell.viewmodel.admin.question.QuestionVm;
import com.tangcco.lockwell.viewmodel.student.vocabularyList.VocabularyListResponseVM;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class VocabularyListServiceImpl implements VocabularyListService {
    @Autowired
    private VocabularyListMapper vocabularyListMapper;
    @Autowired
    private QuestionService questionService;

    protected final static ModelMapper modelMapper = ModelMapperSingle.Instance();
    @Override
    public int insertVocabulary(VocabularyList vocabularyList) {
        return vocabularyListMapper.insertVocabulary(vocabularyList);
    }

    @Override
    public int insertSelective(VocabularyList vocabularyList) {
        return vocabularyListMapper.insertSelective(vocabularyList);
    }

    @Override
    public int updateByPrimaryKeySelective(VocabularyList vocabularyList) {
        return vocabularyListMapper.updateByPrimaryKeySelective(vocabularyList);
    }

    @Override
    public VocabularyList selectVocabularyById(int id) {
        return vocabularyListMapper.selectVocabularyById(id);
    }

    @Override
    public List<VocabularyListResponseVM> selectVocabularyByUserId(int userId) {
        List<VocabularyList> vocabularyLists = vocabularyListMapper.selectVocabularyByUserId(userId);
        List<VocabularyListResponseVM> vocabularyListResponseVMS = BeanUtil.copyToList(vocabularyLists, VocabularyListResponseVM.class);
        vocabularyListResponseVMS.forEach(item -> {
            if(item.getQuestionId()!=null){
                QuestionVm questionVm = questionService.selectById(item.getQuestionId());
                item.setSource(questionVm.getGradeLevel()+"阅读理解");
            }
        });
        return vocabularyListResponseVMS;
    }

    @Override
    public List<VocabularyList> selectVocabularyByQuestionId(int questionId, int userId) {
        return vocabularyListMapper.selectVocabularyByQuestionId(questionId,userId);
    }

    @Override
    public int updateVocabulary(VocabularyList vocabularyList) {
        return vocabularyListMapper.updateVocabulary(vocabularyList);
    }

    @Override
    public int deleteVocabularyById(int id) {
        return vocabularyListMapper.deleteVocabularyById(id);
    }

    @Override
    public List<VocabularyList> selectAllVocabulary() {
        return vocabularyListMapper.selectAllVocabulary();
    }

    @Override
    public List<VocabularyList> selectVocabularyBySituation(String situation , int userId) {
        return vocabularyListMapper.selectVocabularyBySituation(situation,userId);
    }

    @Override
    public List<VocabularyList> selectVocabularyByNewWordLike(String newWord, int userId) {
        return vocabularyListMapper.selectVocabularyByNewWordLike(newWord,userId);
    }

    @Override
    public List<VocabularyList> selectVocabularyByPage(int pageNum, int pageSize) {
        return vocabularyListMapper.selectVocabularyByPage(pageNum,pageSize);
    }

    @Override
    public int countVocabulary(int userId) {
        return vocabularyListMapper.countVocabulary(userId);
    }

    @Override
    public List<VocabularyList> selectVocabularyByUserIdAndSituation(int userId, String situation) {
        return vocabularyListMapper.selectVocabularyByUserIdAndSituation(userId,situation);
    }

    @Override
    public int updateVocabularySituation(int id, String newSituation) {
        return vocabularyListMapper.updateVocabularySituation(id,newSituation);
    }

    @Override
    public int insertVocabularyBatch(List<VocabularyList> vocabularyLists) {
        return vocabularyListMapper.insertVocabularyBatch(vocabularyLists);
    }

    @Override
    public int deleteVocabularyByIds(List<Integer> idList) {
        return vocabularyListMapper.deleteVocabularyByIds(idList);
    }

    @Override
    public PageInfo<VocabularyList> page(Integer userId) {
        PageHelper.startPage(1,10);
        List<VocabularyList> vocabularyLists = vocabularyListMapper.selectVocabularyByUserId(userId);
        PageInfo<VocabularyList> pageInfo = new PageInfo<>(vocabularyLists);
        return pageInfo;
    }

    @Override
    public List<VocabularyList> selectByCondition(VocabularyList vocabularyList) {
        return vocabularyListMapper.selectByCondition(vocabularyList);
    }
}
